<?php
include ("config.php");

function getInfo($i){
    switch($i){
        case 0 :  ;
            break;
        case 1 : return "Tylko zarejestrowani użytkownicy mogą korzystać z tej opcji." ;
            break;
        case 2 : return "Ta opcja dostępna jest tylko dla studentów." ;
            break;
        case 3 : return "Brak wolnych miejsc dla tej oferty - poszukaj innej.";
            break;
        case 4 : return "Aktualnie oczekujesz na potwierdzenie wcześniej wybranej praktyki przez administratora.";
            break;
        case 5 : return "Nie możesz wybrać kolejnej praktyki, przed ukończeniem wcześniejszej.";
        case 6 : return "Wybrałeś praktykę. Zostaniesz poinformowany mailem gdy zostanie potwierdzona przez administratora.";
        case 7 : return "Nieprawidłowy email lub hasło.";
        case 8 : return "Brak praktyki. Aby przejrzeć oferty praktyk przejdź do strony <a href=\"show_practices.php\">ZOBACZ OFERTY</a>";
        case 9 : return "Stare hasło jest nieprawidłowe";
        case 10: return "Hasło zostało zmienione";
        case 11: return "Adres email został zmieniony.";
        case 12: return "Zmieniono numer albumu.";
        case 13: return "Zmieniono wydział i kierunek.";
    }
}
function getRows($tables,$from){
    $query = mysql_query("SELECT $tables FROM `$from` ");
    $rows = array();
    while($row = mysql_fetch_assoc($query)){
        $rows[] = $row;
    }
    return $rows;
}
function getRow($tables,$id,$from){
    $query = mysql_query("SELECT $tables FROM `$from` WHERE id='$id'");
    $rows = array();
    while($row = mysql_fetch_assoc($query)){
        $rows[] = $row;
    }
    return $rows;
}
function getPracticeDetails($id){
    $practice_id = $id;
    $practiceDetails = array();
    $sql1 = mysql_query("SELECT p.* FROM practices p WHERE p.id ='$practice_id' ");

    while($row = mysql_fetch_assoc($sql1)){
            $practiceDetails['practice'] = $row;
    }
    $company_id = $practiceDetails['practice']['company_id'];
    $person_id  = $practiceDetails['practice']['company_person'];

    $sql2 = mysql_query("SELECT c.id,c.name,c.address,c.phone,c.site,c.email FROM user_company c WHERE c.id = '$company_id'");
    while($row = mysql_fetch_assoc($sql2)){
            $practiceDetails['company'] = $row;
    }

    $sql3 = mysql_query("SELECT p.* FROM company_person p WHERE p.id = '$person_id'");
    while($row = mysql_fetch_assoc($sql3)){
            $practiceDetails['person'] = $row;
    }

    return $practiceDetails;
}
function addPractices($data){

    $date_start = $data['date_start'];
    $date_end = $data['date_end'];
    $amount = $data['amount'];
    $paid = $data['paid'];
    if($paid == "on"){
        $paid = 1;
    }
    else{
        $paid = 0;
    }
    $duties = $data['duties'];
    $requirements = $data['requirements'];
    foreach($data['tech'] as $key => $value){
        $technology .= $value.", ";
    }
    $company_id = $data['company_id'];
    $company_person =$data['company_person'];
    $date = date("y-m-d");

    $query = "INSERT INTO `practices` (`add_date`,`date_start`,`date_end`,`amount`,`paid`,`duties`,`requirements`,`technology`,`company_id`,`company_person`)
              VALUES
              ('$date','$date_start','$date_end','$amount','$paid','$duties','$requirements','$technology','$company_id','$company_person')";
    mysql_query($query); 
}
function addStudent($data){
    $email = $data['email'];
    $password = md5($data['password']) ;
    $name = $data['s_name'];
    $surname = $data['s_surname'] ;
    $album_nr = $data['s_album'];
    $faculty_id = $data['s_faculty'];
    $specialization_id = $data['s_specialization'];

    $query = "INSERT INTO `user_student` (`email`,`password`,`name`,`surname`,`album_nr`,`faculty_id`,`specialization_id`)
              VALUES ('$email','$password','$name','$surname','$album_nr','$faculty_id','$specialization_id')";

    mysql_query($query);
}
function addCompany($data){
    echo addCompany_person($data);
    $name = $data['c_person_name'];
    $surname = $data['c_person_surname'];
    $email = $data['c_person_email'];
    $query = mysql_query("SELECT id FROM `company_person` WHERE (name ='$name' AND surname ='$surname' AND email ='$email' )");
    
    $row = mysql_fetch_assoc($query);

    $person_id = $row['id'];
    $email = $data['email'];
    $password = md5($data['password']);
    $name = $data['c_name'];
    $address = $data['c_address'];
    $phone = $data['c_phone'];
    $site = $data['c_site'];

    $query = mysql_query("INSERT INTO `user_company` (`email`,`password`,`name`,`address`,`phone`,`site`,`person_id`)
                          VALUES ('$email','$password','$name','$address','$phone','$site','$person_id')");
}
function addCompany_person($data){
    $name = $data['c_person_name'];
    $surname = $data['c_person_surname'];
    $position = $data['c_person_position'];
    $email = $data['c_person_email'];
    $phone = $data['c_person_phone'];

    $query = mysql_query("INSERT INTO `company_person` (`name`,`surname`,`position`,`email`,`phone`)
              VALUES ('$name','$surname','$position','$email','$phone')");
}
function changePassword($user_type,$id,$oldPass,$newPass){
    $query = mysql_query("SELECT password FROM `$user_type` WHERE id = '$id'");
    $row = mysql_fetch_assoc($query);

    $user_password = $row['password'];
    $oldPass = md5($oldPass);
    $newPass = md5($newPass);

    if($user_password != $oldPass){
        return getInfo(9);
    }else{
        $change_query = mysql_query("UPDATE $user_type SET password = '$newPass' WHERE id='$id'");
        return getInfo(10);
    }
}
function changeEmail($user_type,$id,$new_email){
    $change_query = mysql_query("UPDATE $user_type SET email = '$new_email' WHERE id='$id'");
    return getInfo(11);
}
function changeAlbum($user_type,$id,$new_album){
    $change_query = mysql_query("UPDATE $user_type SET album_nr = '$new_album' WHERE id='$id'");
    return getInfo(12);
}
function changeFaculty($id,$faculty_id,$specialization_id){
    $change_query = mysql_query("UPDATE user_student SET faculty_id = '$faculty_id' , specialization_id = '$specialization_id'
            WHERE id = '$id'");
    return getInfo(13);
}
?>
